// pages/luckydraw/luckydraw.js
const app = getApp()
var utils = require('../../utils/util.js');
var interval = null //倒计时函数
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		ishedden:false,
		time: '获取验证码', //倒计时 
		currentTime: 60, //限制60s
		isClick: false, //获取验证码按钮，默认允许点击
		username:"",
		currentPage:1
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		this.memberInfo();
		this.goodsList();
		this.memberGoodsList();
		this.systemInfo();
	},

	//获取用户信息
	memberInfo: function (e) {
		var that = this;
		utils.http("/api/v1/member/index", {}, function (res) {
			//  console.log(res)
			that.setData({
				info: res.datas
			})
		})
	},
	//抽奖列表
	goodsList:function(e){
		var that = this;
		utils.http("/api/v1/goods/goods_list", {}, function (res) {
			//  console.log(res)
			that.setData({
				goods_list: res.datas
			})
		})
	},
	//抽奖弹窗  我要抽奖
	goodsListTap:function(e){
		console.log(e);
		var that = this;
		if (that.data.info.last_luck_num==0){
			wx.showToast({
				title: '您的抽奖次数已用完',
				icon:"none"
			})
			return false
		}
		
		utils.showLoading("抽奖中...")
		that.setData({
			name:that.data.goods_list[e.currentTarget.dataset.index].name
		})
		utils.http("/api/v1/goods/luck_goods", {
			luck_goods_id: that.data.goods_list[e.currentTarget.dataset.index].id
		}, function (res) {
			utils.hideLoading();
			if (res.state==1){
				
				if (that.data.goods_list[e.currentTarget.dataset.index].if_kong == 1) {
					wx.showToast({
						title: '很遗憾，您没有中奖',
						icon: "none"
					})
					return false
				} else {
					that.setData({
						ishedden: true,
						id: that.data.goods_list[e.currentTarget.dataset.index].id
					})
				}
				that.memberInfo();
			}
		})
	},
	//取消弹窗
	formReset:function(e){
		var that=this;
		that.setData({
			ishedden: false
		})
	},
	//我的奖品
	luckydrawTap:function(e){
		wx.navigateTo({
			url: '../prize/prize',
		})
	},
	/**
		* 用户手机号
		* */
	usernameInput: function (event) {
		// console.log("username==",event.detail.value)
		this.setData({
			username: event.detail.value
		})
	},
	/**
     * 获取验证码
     */
	gainAuthCodeAction: function () {
		let that = this;
		/*第一步：验证手机号码*/
		var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/; // 判断手机号码的正则
		if (that.data.username.length == 0) {
			utils.showSuccess('手机号码不能为空', 'none', false, 800);
			return;
		}

		if (that.data.username.length < 11) {
			utils.showSuccess('手机号码长度有误！', 'none', false, 800);
			return;
		}

		if (!myreg.test(that.data.username)) {
			utils.showSuccess('错误的手机号码！', 'none', false, 800);
			return;
		}
		/*第二步：设置计时器*/
		// 先禁止获取验证码按钮的点击
		that.setData({
			isClick: true,
		})
		// 60s倒计时 setInterval功能用于循环，常常用于播放动画，或者时间显示
		var currentTime = that.data.currentTime;
		interval = setInterval(function () {
			currentTime--; //减
			that.setData({
				time: currentTime + '秒后获取'
			})
			if (currentTime <= 0) {
				clearInterval(interval)
				that.setData({
					time: '获取验证码',
					currentTime: 60,
					isClick: false
				})
			}
		}, 1000);
		/*第三步：请求验证码接口，并记录服务器返回的验证码用于判断，这里服务器也可能不返回验证码，那验证码的判断交给后台*/
		// wx.request({})
		utils.http("/api/v1/common/identifying_code", {
			phone: that.data.username
		}, function (res) {
			utils.hideLoading();
			if (res.state == 1) {

			} else {
				utils.message(res.datas);
			}
		});
	},

	//登录
	formSubmit:function(e){
		console.log(e);
		let that = this;
		/*第一步：验证手机号码*/
		var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/; // 判断手机号码的正则
		if (e.detail.value.username.length == 0) {
			utils.showSuccess('手机号码不能为空', 'none', false, 800);
			return;
		}

		if (e.detail.value.username.length < 11) {
			utils.showSuccess('手机号码长度有误！', 'none', false, 800);
			return;
		}

		if (!myreg.test(e.detail.value.username)) {
			utils.showSuccess('错误的手机号码！', 'none', false, 800);
			return;
		}
		if (e.detail.value.code.length == 0) {
			utils.showSuccess('请输入验证码', 'none', false, 800);
			return;
		}

		utils.http("/api/v1/goods/add_member_goods",{
			member_phone: e.detail.value.username,
			code: e.detail.value.code,
			luck_goods_id:that.data.id
		},function(res){
			if (res.state==1){
				utils.showSuccess('领取成功', 'none', false, 800);
				that.setData({
					ishedden: false
				})
			}
		})

	},

	//中奖名单
	memberGoodsList:function(){
		var that = this;
		utils.http("/api/v1/goods/member_goods_list", {
			page: that.data.currentPage
		}, function (res) {
		//	phone = phone.replace(/(\d{3})(\d{4})(\d{4})/, "$1****$3");
			var allpost=that.data.membergoodslist||[];
			for(var i in res.datas){
				res.datas[i].member_phone=res.datas[i].member_phone.replace(/(\d{3})(\d{4})(\d{4})/, "$1****$3")
				allpost.push(res.datas[i])
			}
			if (res.datas.length < 10) {
				that.setData({
					tishi_title: '已加载全部',
				});
			} else {
				that.setData({
					tishi_title: '加载更多',
				});
			}
			that.setData({
				membergoodslist: allpost,
				ready: res.datas.length == 0 ? 1 : 2,
			})
		//	console.log(that.data.membergoodslist)
		})
	},

	//活动规则
	systemInfo:function(e){
		var that = this;
		utils.http("/api/v1/common/system", {}, function (res) {
			console.log(res)
			that.setData({
				systeminfo: res.datas
			})
		})
	},
	upper: function (e) {
		console.log(e,"upper")
	},
	lower: function (e) {
		console.log(e,"lower")
		var that = this;
		if (that.data.ready == 2) {
			var page = that.data.currentPage + 1
			that.setData({
				currentPage: page,
			})
			that.memberGoodsList()
		}

	},

	onShow: function (e) {
	
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},


	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})